package com.ltsznh.android.familybooks.account;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.ltsznh.android.familybooks.MainActivity;
import com.ltsznh.android.familybooks.R;
import com.ltsznh.android.familybooks.db.dbo;

public class AccountCategoryActivity extends Activity {

    private ArrayAdapter<String> accountCategoryAdapter;
    private int CATEGORY_LEVEL = 1;

    ListView categoryListView;
    private String sqlString;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category);

        ActionBar actionBar = this.getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        accountCategoryAdapter = new ArrayAdapter<String>(this, R.layout.category);
        categoryListView = (ListView)findViewById(R.id.listView_category);
        categoryListView.setAdapter(accountCategoryAdapter);
        refreshData();

        initCategoryEdit();
    }

    @Override
    protected void onStart() {
        super.onStart();
        refreshData();
    }

    public void refreshData(){
        dbo.getReadableDatabase(this);
        sqlString = "select distinct accountCategory from inv_account order by displayOrder";
        Cursor categoryCursor = dbo.fbSQLite.rawQuery(sqlString, null);
        accountCategoryAdapter.clear();
        if(categoryCursor.getCount() > 0){
            categoryCursor.moveToFirst();
            do{
                accountCategoryAdapter.add(categoryCursor.getString(0));
            }while (categoryCursor.moveToNext());
        }
        categoryCursor.close();
        dbo.closeDatabase();

    }

    private void initCategoryEdit(){
        categoryListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                String accountCategory = accountCategoryAdapter.getItem(i).toString();
                AccountCategoryEditDialog accountCategoryEditDialog = AccountCategoryEditDialog.newInstance(accountCategory);
                accountCategoryEditDialog.setArgs(AccountCategoryActivity.this,"inv_account","accountCategory","accountCategory = '" + accountCategory + "'");
                accountCategoryEditDialog.show(getFragmentManager(), null);
                return false;
            }
        });

        categoryListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(AccountCategoryActivity.this,AccountActivity.class);
                intent.putExtra("accountCategory",accountCategoryAdapter.getItem(i).toString());
                startActivity(intent);
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId())
        {
            case android.R.id.home:
                Intent intent = new Intent(this, MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                return true;
            default:
                return false;
        }
    }

//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        // Inflate the menu; this adds items to the action bar if it is present.
//        getMenuInflater().inflate(R.menu.category, menu);
//        return true;
//    }


}
